package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.MainActivity;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.bean.ColumnArticleBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 添加文章简介及栏目activity
 */

public class SelectColumn extends AppCompatActivity implements View.OnClickListener {

    private String mEditor, ed_title;
    FlowLayout flow;
    private ColumnArticleBean columnBean;
    private EditText article_describe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_column_actvivity);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        mEditor = intent.getStringExtra("mEditor");
        ed_title = intent.getStringExtra("ed_title");
        flow = (FlowLayout) findViewById(R.id.flow);
        article_describe = (EditText) findViewById(R.id.article_describe);
        findViewById(R.id.column_image).setOnClickListener(this);
        findViewById(R.id.column_btn).setOnClickListener(this);
        initFlow();
    }

    private void initFlow() {
        String url = AppUrl.URL + AppUrl.Column;
        XUtils.Get(url, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                columnBean = gson.fromJson(result, ColumnArticleBean.class);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                for (int i = 0; i < columnBean.getColumn().size(); i++) {
                    addTextView(columnBean.getColumn().get(i), i, -1);
                }
            }
        });
        // 设置条目的水平间距
        flow.setChildViewHorizontalPadding(DensityUtil.dip2px(15f));
        // 设置每行的垂直间距
        flow.setChildViewVerticalPadding(DensityUtil.dip2px(10f));
//        final List<String> test = new ArrayList<>();
//        test.add("文艺");
//        test.add("情感");
//        test.add("励志");
//        test.add("心理");
//        test.add("惊悚");
//        test.add("青年");
//        test.add("科技");
    }

    TextView textView;

    private void addTextView(final ColumnArticleBean.ColumnBean str, final int j, final int index) {
        textView = new TextView(this);
        textView.setText(str.getColumnname());
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(12);
        textView.setPadding(DensityUtil.dip2px(12f),
                DensityUtil.dip2px(7f),
                DensityUtil.dip2px(12f)
                , DensityUtil.dip2px(7f));
        textView.setBackgroundResource(R.drawable.b_search_bg);
        textView.setTextColor(ContextCompat.getColor(this, R.color.black));
        if (index != 1) {
            textView.setBackgroundResource(R.drawable.b9_circle_white2);
            textView.setTextColor(Color.parseColor("#000000"));
        } else {
            textView.setBackgroundResource(R.drawable.b9_circle_main);
            textView.setTextColor(Color.parseColor("#FFFFFF"));
        }
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 flow.removeAllViews();
                for (int i = 0; i < columnBean.getColumn().size(); i++) {

                    if (i == j) {
                        //textView.setBackgroundResource(R.drawable.b9_circle_main);
                        columnBean.getColumn().get(i).setState(1);
                        addTextView(columnBean.getColumn().get(i), i, 1);
                    } else {
                        columnBean.getColumn().get(i).setState(0);
                        addTextView(columnBean.getColumn().get(i), i, 0);
                    }
                }
//                if (str.getState() == 0) {
//
//                } else {
//                    textView.setBackgroundResource(R.drawable.b9_circle_white2);
//                    str.setState(0);
//                }
               // Toast.makeText(SelectColumn.this, textView.getText() + "", Toast.LENGTH_SHORT).show();
            }
        });
        flow.addView(textView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.column_image:
                finish();
                break;
            case R.id.column_btn:
                String str="";
                for (int i = 0; i < columnBean.getColumn().size(); i++) {

                    if (columnBean.getColumn().get(i).getState()==1) {
                        str=columnBean.getColumn().get(i).getColumnname();
                    }

                }
//                Toast.makeText(SelectColumn.this, str + "", Toast.LENGTH_SHORT).show();
                if (article_describe.getText().equals("") || article_describe.getText() == null) {
                    Toast.makeText(this, "文章简介不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(str==null||str.equals("")){
                    Toast.makeText(this, "请选择所属栏目", Toast.LENGTH_SHORT).show();
                    return;
                }
                String uri = AppUrl.URL + AppUrl.Add_article;
                Map<String, String> map = new HashMap<>();
                map.put("articleTitle", ed_title);
                map.put("articleContent", mEditor);
                map.put("articleDescribe", article_describe.getText().toString());
                map.put("articleColumn", str);
                map.put("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
                Set keys = map.keySet();
                if (keys != null) {
                    Iterator iterator = keys.iterator();
                    while (iterator.hasNext()) {
                        Object key = iterator.next();
                        Object value = map.get(key);
                        Log.e("----zf" + key, "" + value + "\n");
                    }
                }
                XUtils.Get(uri, map, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        String results = null;
                        String code = null;
                        try {
                            JSONObject json = new JSONObject(result);
                            results = json.optString("result");
                            code = json.optString("code");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if(results.equals("true")){
                            Toast.makeText(SelectColumn.this, "发布成功", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(SelectColumn.this, MainActivity.class));
                            finish();
                        }else{
                            Toast.makeText(SelectColumn.this, "发布失败", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        Toast.makeText(SelectColumn.this, "发布文章还在完善中", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFinished() {
                        super.onFinished();
                    }
                });

                break;
        }
    }
}
